package cn.org.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.org.domain.Org;
import cn.org.domain.Person;
import cn.org.domain.PersonOrgPos;
import cn.org.domain.Position;
import cn.org.service.IOrgService;
import cn.org.service.IPersonService;
import cn.org.service.IPositionService;
import cn.org.utils.JsonUtils;

@Controller
public class PersonController {
	
	@Resource
	private IOrgService orgService;
	
	@Resource
	private IPersonService personService;
	
	@Resource
	private IPositionService positionService;
	
	@RequestMapping("/personlist")
	public String toIndex(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		//if(user==null) System.out.println("hahahahaahahahah");
		List<Org> list=orgService.getOrgByPerson(user);
		//System.out.println(list.get(0));
		session.setAttribute("org", list.get(0));
		return "user";
	}
	@RequestMapping("/compersonlist")
	public String compersonList(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		//if(user==null) System.out.println("hahahahaahahahah");
		List<Org> list=orgService.getOrgByPerson(user);
		//System.out.println(list.get(0));
		session.setAttribute("org", list.get(0));
		return "usershow";
	}
	@RequestMapping("/childpersonlist")
	public String listchildPerson(@RequestParam("oid") int oid,HttpSession session,Model model){
		Org o=orgService.selectByPrimaryKey(oid);
		session.setAttribute("org", o);
		//System.out.println(o);
		//System.out.println(JsonUtils.list2json(childList));
		
		return "user";
	}
	@RequestMapping("/comchildpersonlist")
	public String listcomchildPerson(@RequestParam("oid") int oid,HttpSession session,Model model){
		Org o=orgService.selectByPrimaryKey(oid);
		session.setAttribute("org", o);
		//System.out.println(o);
		//System.out.println(JsonUtils.list2json(childList));
		
		return "usershow";
	}
	@RequestMapping(value="/listperson",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListPerson(HttpSession session){
		
		Org org=(Org) session.getAttribute("org");
	//	System.out.println("---------ListPerson-----------");
		//System.out.println(org);
		List<Person> childList=new ArrayList<Person>();
		childList=getChildPersons(childList,org);
		//System.out.println(JsonUtils.list2json(childList));
		//System.out.println(childList.size());
		String json="[";
		for(int i=0;i<childList.size();i++){
			Person p=childList.get(i);
			for(int j=0;j<orgService.getOrgByPerson(p).size();j++){
			Org o=orgService.getOrgByPerson(p).get(j);
			if(o.getLevel()>=org.getLevel()){
			int adminflag=0;
			Position pos=positionService.getPositionByPerson(p).get(j);
			List<PersonOrgPos> adminlist=personService.selectByPerson(p);
			for(PersonOrgPos pop:adminlist){
				if(pop.getAdminOrgLevel()==1) adminflag=1;
			}
			if(i<childList.size()-1)
			 json=json+"{\"id\":\""+p.getId()+"\",\"name\":\""+p.getName()+"\",\"phone\":\""+p.getPhone()+"\",\"sex\":\""+p.getSex()+"\",\"sn\":\""+p.getSn()+"\",\"org\":\""+o.getName()+"\",\"pos\":\""+pos.getName()+"\",\"pid\":\""+pos.getId()+"\",\"oid\":\""+o.getId()+"\",\"adminflag\":\""+adminflag+"\"},";
			else
				 json=json+"{\"id\":\""+p.getId()+"\",\"name\":\""+p.getName()+"\",\"phone\":\""+p.getPhone()+"\",\"sex\":\""+p.getSex()+"\",\"sn\":\""+p.getSn()+"\",\"org\":\""+o.getName()+"\",\"pos\":\""+pos.getName()+"\",\"pid\":\""+pos.getId()+"\",\"oid\":\""+o.getId()+"\",\"adminflag\":\""+adminflag+"\"}";
		}
			}
		}
		json=json+"]";
	//	System.out.println("listorg:--------->"+json);
		return json;
		
		
		
	}
/*	@RequestMapping(value="/listpersontree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListPersonTree(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		Org org=orgService.getOrgByPerson(user).get(0);
	//	System.out.println("---------ListPersonTree-----------");
	//	System.out.println(org);
		List<Org> childorgList=new ArrayList<Org>();
		childorgList=getChildOrgs(childorgList,org);
		org.setPid(0);
		childorgList.add(org);
		List<Org> childorgListtemp=new ArrayList<Org>();
		for(Org o :childorgList){
			System.out.println("all:---------->"+o);
		}
		for(int i=0;i<childorgList.size();i++){
		//	System.out.println("LOOK!!"+childorgList.size());
			Org o=childorgList.get(i);
		//	System.out.println(o);
			List<Person> list=new ArrayList<Person>();
			list=getChildPersons(list,o);
			for(Person p :list){
				System.out.println(o.getName()+":---------->"+p);
			}
			if(!list.isEmpty()){
				childorgListtemp.add(o);
			//	System.out.println("add:--------->"+o);
			}
		}
		for(Org o :childorgListtemp){
			System.out.println(o);
		}
		List<Person> childpersonList=new ArrayList<Person>();
		childpersonList=getChildPersons(childpersonList,org);
		
	//	System.out.println(PersonOrgList2Tree(childpersonList,childorgListtemp));
		return PersonOrgList2Tree(childpersonList,childorgListtemp);
	}*/
	@RequestMapping(value="/listpersontree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListOrgTree(HttpSession session){
		Person user=(Person) session.getAttribute("user");
		Org org=orgService.getOrgByPerson(user).get(0);
		////System.out.println(org);
		////System.out.println("---------ListOrgTree-----------");
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		org.setPid(0);
		childList.add(org);
		
	
		//System.out.println(JsonUtils.OrgList2Tree(childList));
		return JsonUtils.PersonList2Tree(childList);
	}
	@RequestMapping(value="/listcompersontree",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ListComPersonTree(){
		Org org=orgService.selectByName("宁波市");
		////System.out.println(org);
		////System.out.println("---------ListOrgTree-----------");
		List<Org> childList=new ArrayList<Org>();
		childList=getChildOrgs(childList,org);
		org.setPid(0);
		childList.add(org);
		
	
		//System.out.println(JsonUtils.OrgList2Tree(childList));
		return JsonUtils.ComPersonList2Tree(childList);
	}
	
	@RequestMapping(value="/addperson",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String AddPerson(@RequestParam("oid")int oid,@RequestParam("pid") int pid,Person person){
	//	System.out.println(person);
		personService.insert(person);
		/*
		 * 添加部门数据库操作
		 */
		Person p=personService.getPersonByName(person.getName());
		PersonOrgPos pop=new PersonOrgPos();
		pop.setOrgId(oid);
		pop.setPersonId(p.getId());
		pop.setPosId(pid);
		personService.insertPersonOrgPos(pop);
		return "{\"data\":\"success\"}";
	}
	
	@RequestMapping(value="/getparentorgpos",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String GetParentOrg(HttpSession session){
		Org org=(Org) session.getAttribute("org");
		List<Org> list=new ArrayList<Org>();
		list.add(org);
		list=getChildOrgs(list,org);
		List<Position> listpos=positionService.getAllPosition();
		List result=new ArrayList();
		result.add(list);
		result.add(listpos);
		System.out.println(JsonUtils.list2json(result));
		return JsonUtils.list2json(result);
	}
	
	@RequestMapping(value="/editperson",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String EditPerson(@RequestParam("pid") int pid,HttpSession session){
		Org org=(Org) session.getAttribute("org");
		List<Org> list=new ArrayList<Org>();
		list.add(org);
		list=getChildOrgs(list,org);
		List<Position> listpos=positionService.getAllPosition();
		Person p=personService.selectByPrimaryKey(pid);
		Position pos=positionService.getPositionByPerson(p).get(0);
		Org o=orgService.getOrgByPerson(p).get(0);
		String json="[";
		json=json+"{\"id\":\""+p.getId()+"\",\"name\":\""+p.getName()+"\",\"phone\":\""+p.getPhone()+"\",\"sex\":\""+p.getSex()+"\",\"sn\":\""+p.getSn()+"\",\"org\":\""+o.getName()+"\",\"pos\":\""+pos.getName()+"\",\"pid\":\""+pos.getId()+"\",\"oid\":\""+o.getId()+"\"},"+JsonUtils.list2json(list)+","+JsonUtils.list2json(listpos)+"]";
	//	System.out.println(json);
		return json;
	}
	
	@RequestMapping(value="/confirmeditperson",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String ConfirmEditPerson(@RequestParam("pid") int pid,@RequestParam("oid") int oid,@RequestParam("pastpid") int pastpid,@RequestParam("pastoid") int pastoid,Person person){
		//System.out.println(org);
		/*
		 * 编辑部门数据库操作
		 */
		personService.updateByPrimaryKey(person);
		PersonOrgPos pop=personService.selectByPOP(pastoid, person.getId(), pastpid);
		pop.setOrgId(oid);
		pop.setPosId(pid);
		personService.updateByPrimaryKey(pop);
		return "{\"data\":\"success\"}";
	}
	
	@RequestMapping(value="/delperson",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String DelPerson(@RequestParam("pid") int pid){
		//System.out.println(org);
		/*
		 * 删除部门数据库操作
		 */
		Person p=personService.selectByPrimaryKey(pid);
		Org org=orgService.getOrgByPerson(p).get(0);
		Position pos=positionService.getPositionByPerson(p).get(0);
		PersonOrgPos pop=personService.selectByPOP(org.getId(), p.getId(), pos.getId());
		personService.deletePop(pop.getId());
		personService.deleteByPrimaryKey(p.getId());
		return "{\"data\":\"success\"}";
	}  
	
	@RequestMapping(value="/adminset",method=RequestMethod.POST,produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String AdminSet(@RequestParam("id") int id,@RequestParam("pid") int pid,@RequestParam("oid") int oid){
		PersonOrgPos pop=personService.selectByPOP(oid, id, pid);
		System.out.println(pop);
		if(pop.getAdminOrgLevel()==1) pop.setAdminOrgLevel(0);
		else pop.setAdminOrgLevel(1);
		personService.updateByPrimaryKey(pop);
		return "{\"data\":\"success\"}";
	}  
	private List<Org> getChildOrgs(List<Org> resultlist,Org o ){
		List<Org> list=orgService.getAllChildOrg(o);
		if(!list.isEmpty()){
		for(Org org:list){
		resultlist.add(org);
		 getChildOrgs(resultlist,org);	
		}
		}
		return resultlist;
	}
	
	private List<Person> getChildPersons(List<Person> resultlist,Org o ){
		List<Org> list=orgService.getAllChildOrg(o);
		resultlist.addAll(personService.getPersonsByOrg(o));
		if(!list.isEmpty()){
		for(Org org:list){
		
		//resultlist.addAll(personService.getPersonsByOrg(org));
		getChildPersons(resultlist,org);	
		}
		}   
		HashSet<Person> temp=new HashSet<Person>(resultlist);
		resultlist.clear();
		resultlist.addAll(temp);
	//	System.out.println(resultlist.size());
		return resultlist;
	}
	private  String PersonOrgList2Tree(List<Person> plist,List<Org> olist){
		String tree="[";
		for(int i=0;i<plist.size();i++){
			Person person=plist.get(i);
			List<Org> orglist=orgService.getOrgByPerson(person);
			for(int j=0;j<orglist.size();j++){
		tree=tree+"{\"id\":"+person.getId()+", \"pId\":"+orgService.getOrgByPerson(person).get(j).getId()+", \"name\":\""+person.getName()+"\", \"icon\":\"./css/zTreeStyle/img/diy/user.png\"},";
			}	
			}
		for(int i=0;i<olist.size();i++){
			Org org=olist.get(i);
			if(i<olist.size()-1)
				  tree=tree+"{\"id\":"+org.getId()+", \"pId\":"+org.getPid()+", \"name\":\""+org.getName()+"\", \"url\":\"childpersonlist?oid="+org.getId()+"\",\"target\":\"_self\",\"open\":true, \"icon\":\"./css/zTreeStyle/img/diy/1_open.png\", \"iconClose\":\"./css/zTreeStyle/img/diy/1_close.png\"},";
				else tree=tree+"{\"id\":"+org.getId()+", \"pId\":"+org.getPid()+", \"name\":\""+org.getName()+"\", \"url\":\"childpersonlist?oid="+org.getId()+"\",\"target\":\"_self\",\"open\":true, \"icon\":\"./css/zTreeStyle/img/diy/1_open.png\", \"iconClose\":\"./css/zTreeStyle/img/diy/1_close.png\"}";
				}
				tree=tree+"]";
		return tree;
		
	}
	
}
